var SCOPE = {}; // 定义全局对象  所有全局变量都放到这个对象里面，防止污染上下文
SCOPE.upload_img_url = '/index/base/upload_image'; //  定义上传url
SCOPE.upload_img_type = '0';  // 初始化上传图片类型
SCOPE.upload_img_obj = {};  // 定义上传图片元素对象
SCOPE.upload_img_num = 0;  // 定义上传图片数量
SCOPE.upload_img_max_num = 3; // 图片上传上限
SCOPE.get_qiniu_token_url = '/index/base/get_qiniu_token';  // 获取七牛云token
SCOPE.qiniu_up_url = 'http://up.qiniu.com';  // 七牛云上传接口

$(function () {
    // 导航切换
    $('.msui_navTop-cnav').click(function (e) {
        e.preventDefault();
        $('.index-nav').css('display','block');
        $('.index-nav-bd').animate({
            right:'.3rem'

        });
    });
    // 隐藏导航
    $('.layer').click(function (e) {
        e.preventDefault();
        $('.index-nav-bd').animate({
            right:'-50%'
        },function () {
            $('.index-nav').css('display','none');
        });
    });
    // 查看大图片
    $(document).on('click','img.viewPhone',function (e) {
        e.preventDefault();
        var imgmain=[];
        var tt=$(this).parents('.viewImage').find('img.viewPhone');
        var countActive=$(this).attr('data-img');
        countActive=parseFloat(countActive);
        var count=tt.length;
        console.log(count);
        for (var i=0;i<count;i++){
            imgmain.push(tt[i].alt);
        }
        console.log(imgmain);
        var pb1 = $.photoBrowser({
            items:imgmain,
            onSlideChange: function(index) {
                console.log(index);
                console.log()
            },
            onOpen: function() {

            },
            onClose: function() {

            },
            initIndex:countActive
        });
        pb1.open();
        $(`.weui-photo-browser-modal.weui-photo-browser-modal-visible .swiper-wrapper .swiper-slide:nth-of-type(${countActive+1})`).addClass('swiper-slide-active');
    });
    // 标题高度
    listFont();
    $(".sweepLight").css({

    });

    /**
     * 点击获取验证码按钮
     */
    $('.js-verify').click(function(){
        var _this = $(this);

        // 验证手机号码
        var mobile = $('input[name="mobile"]').val();
        if (!mobile) {
            $.toptip('请输入手机号码', 'error');
            return false;
        }
        var myreg = /^0?(13|14|15|17|18|19)[0-9]{9}$/;
        if(!myreg.test(mobile)){
            $.toptip('请输入正确的手机号码', 'error');
            return false;
        }

        $.get('/index/base/send_code', {mobile : mobile}, function (msg) {
            if(msg.code == 200) {
                $.toptip('发送成功', 'success');
                verifyCode(_this,60);
            } else {
                $.toptip(msg.msg, 'error');
            }
        }, 'json');
    });
});

// tab切换
$(function() {
    var baselineWidth=0,
        msuiMenuLiWith=0,
        _li_width=0,
        _li_width_4=0,
        msuiMenuLiWithList=[];

    // console.log(SCOPE.obj);
    if (SCOPE.obj !== undefined) {
        // 进入页面根据描点显示内容
        var _hash = window.location.hash || '#hash' + SCOPE.obj.hash + '_';
        $("a[data-hash='" + _hash + "']").addClass('msui_tabMenu-active');
    }
    start();
    get_list_by_scope_obj();

    /*dispaly:none进行切换*/
    $('.js-menu').on('click',"li", function () {
        // $(".js-list").html('');
        var dataTab = $(this).attr('data-tab');
        var that = $(this);
        var baseline=0;
        for (var i=0;i<dataTab;i++){
            baseline+=msuiMenuLiWithList[i];
        }
        $(".weui-baseline").animate({marginLeft:baseline}).css('width',msuiMenuLiWithList[dataTab]-20+'px');
        $(that).children().addClass('msui_tabMenu-active');
        that.siblings().children().removeClass('msui_tabMenu-active');
        console.log(that.find('a').text());
        if (SCOPE.obj !== undefined) {
            SCOPE.obj.page = 1;
            if (SCOPE.obj.action == 'activity') {
                SCOPE.obj.data.type = that.find('a').data('type');
                SCOPE.obj.data.status = that.find('a').data('status');
            } else if (SCOPE.obj.action == 'shop_product') {
                SCOPE.obj.data.category_id = that.find('a').data('category-id');
            } else if (SCOPE.obj.action == 'friend') {
                SCOPE.obj.data.tag = that.find('a').data('status');
                SCOPE.obj.data.type = that.find('a').data('type');
            }
            get_list_by_scope_obj();
        } else {
            $('.js-list').each(function () {
                $(this).hide();
                if ($(this).attr('dataOrder') == $(that).attr('data-tab')) {
                    $(this).show(400);
                }
            });
        }
    });
    // 有用到swiper，display:block切换
    $('.mtab_list1 .js-list1.active').css({
        position:'absolute',
        left:'0',
        right:'0',
    });
    $('.js-slider').on('click',"li", function () {
        var dataTab = $(this).attr('data-tab');
        var that = $(this);
        var baseline=0;
        for (var i=0;i<dataTab;i++){
            baseline+=msuiMenuLiWithList[i];
        }
        $(".weui-baseline").animate({marginLeft:baseline}).css('width',msuiMenuLiWithList[dataTab]-20+'px');
        $(that).children().addClass('msui_tabMenu-active');
        that.siblings().children().removeClass('msui_tabMenu-active');

        if (SCOPE.obj !== undefined) {
            SCOPE.obj.page = 1;
            if (SCOPE.obj.action == 'friend') {
                SCOPE.obj.data.tag = that.find('a').data('status');
                SCOPE.obj.data.type = that.find('a').data('type');
            } else if (SCOPE.obj.action == 'product_list') {
                SCOPE.obj.data.category_id = that.find('a').data('category-id');
            } else if (SCOPE.obj.action == 'activity_member') {
                SCOPE.obj.data.status = that.find('a').data('status');
            }
            get_list_by_scope_obj();
        } else {

        }
    });
    // tab切换函数
    function start() {
        $('.msui_menu-li').each(function () {
            var t=parseFloat($(this).css('width'));
            msuiMenuLiWith+=t;
            msuiMenuLiWithList.push(t);
        });
        $('.msui_menu-bd').css('width',msuiMenuLiWith+2);
        var t=$(".msui_tabMenu-active");
        var count=t.parent().attr('data-tab');
        for (var i=0;i<count;i++){
            baselineWidth+=msuiMenuLiWithList[i];
        }
        _li_width=msuiMenuLiWithList[count];
        for (var i=0;i<=count;i++){
            _li_width_4+=msuiMenuLiWithList[i];
        }
        $(".weui-baseline").animate({marginLeft:baselineWidth}).css('width',msuiMenuLiWithList[count]-20+'px');
        var _w_width = $(window).width();
        if (_w_width - msuiMenuLiWith < _li_width) {
            $(".msui_menu-mask").scrollLeft(_li_width_4-_w_width);
        }
        if (SCOPE.obj !== undefined) {
            if (SCOPE.obj.action == 'activity') {
                SCOPE.obj.data.type = t.data('type');
                SCOPE.obj.data.status = t.data('status');
            } else if (SCOPE.obj.action == 'friend') {
                SCOPE.obj.data.tag = t.data('status');
                SCOPE.obj.data.type = t.data('type');
            } else if (SCOPE.obj.action == 'shop_product') {
                console.log(t);
                SCOPE.obj.data.category_id = t.data('category-id');
            } else if (SCOPE.obj.action == 'product_list') {
                console.log(t);
                SCOPE.obj.data.category_id = t.data('category-id');
            } else if (SCOPE.obj.action == 'activity_member') {
                console.log(t);
                SCOPE.obj.data.status = t.data('status');
            }
        }
    }
});
// 标题高度函数
function listFont() {
    $(".commodity").each(function () {
        var  that=$(this);
        var length=that.find('.fashionable-list').length;
        for(var i=0;i<length;i+=2){
            var height1=that.find(`.fashionable-list:eq(${i}) .listFont`).css('height');
            var height2=that.find(`.fashionable-list:eq(${i+1}) .listFont`).css('height');
            if(height1>height2){
                that.find(`.fashionable-list:eq(${i}) .listFont`).css('height',height1);
                that.find(`.fashionable-list:eq(${i+1}) .listFont`).css('height',height1);
            }else if(height1<height2){
                that.find(`.fashionable-list:eq(${i}) .listFont`).css('height',height2);
                that.find(`.fashionable-list:eq(${i+1}) .listFont`).css('height',height2);
            }else if(height1==height2){
                that.find(`.fashionable-list:eq(${i}) .listFont`).css('height',height2);
                that.find(`.fashionable-list:eq(${i+1}) .listFont`).css('height',height2);
            }
        }
    })
}

